﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;

namespace Hishop.Plugins.Payment.ChinaUnionPay
{

    [Plugin("银联在线")]
    public class WP_Bank : PaymentRequest
    {
        public WP_Bank(
            string orderId, decimal amount,
            string subject, string body, string buyerEmail, DateTime date,
            string showUrl, string returnUrl, string notifyUrl, string attach)
        {
            v_oid = orderId;
            string temp_amount = (Math.Round(amount * 100, 0).ToString());
            if (temp_amount.Length < 12)
            {
                int dtx = 12 - temp_amount.Length;
                for (; dtx > 0; dtx--)
                {
                    temp_amount = "0" + temp_amount;
                }
            }
            v_amount = temp_amount;
            v_returnUrl = returnUrl;
            v_date = date.ToString("yyyyMMdd");
            v_notifyUrl = notifyUrl;
            Remark1 = attach;
        }

        public WP_Bank()
        {
        }

        #region 常量
 
        private const string Gateway = "https://payment.chinapay.com/pay/TransGet";
        private const string v_moneytype = "156";
        private string Remark1 = "Bankunion";
        #endregion

      

        /// <summary>
        /// 商户名称
        /// </summary>
        [ConfigElement("商户名称", Nullable = false)]
        public string VmName { get; set; }

        /// <summary>
        /// 商户密钥
        /// </summary>
        [ConfigElement("商户密钥", Nullable = false)]
        public string Key { get; set; }              //账户的支付宝安全校验码

        protected override bool NeedProtect
        {
            get { return true; }
        }

        /// <summary>
        /// 订单编号
        /// </summary>
        private readonly string v_oid = "";

        /// <summary>
        /// 交易金额 单位：分
        /// </summary>
        private readonly string v_amount = "";

        /// <summary>
        /// 交易时间
        /// </summary>
        private readonly string v_date = "";

        /// <summary>
        /// 前台回调商户URL
        /// </summary>
        private readonly string v_returnUrl = "";

        /// <summary>
        /// 后台回调商户URL
        /// </summary>
        private readonly string v_notifyUrl = "";

        private string[] ValueVo
        {
            get;
            set;
        }
        private string Plan
        {
            get;
            set;
        }
        public override void SendRequest()
        {
            // 生成签名
           
            WP_QucickPayConf.merName = VmName;
            Plan = WP_QucickPayConf.merCode + v_oid + v_amount + v_moneytype + v_date + "0001" + WP_QucickPayConf.version + v_notifyUrl + v_returnUrl + WP_QucickPayConf.gateid + Remark1;
            //商户需要组装如下对象的数据
            string[] valueVo = new string[]{
             
                WP_QucickPayConf.merCode,//商户代码
                v_oid,//订单号
                v_amount,//金额
                v_moneytype,//货币代码
                v_date,//交易时间
                "0001",//交易类型
                WP_QucickPayConf.version,
                "8607",//网关号
                  v_notifyUrl,//后台回调商户URL
                v_returnUrl,//前台回调商户URL
              
                 Remark1,
			};
            ValueVo = valueVo;
            WP_QucickPayConf.gateWay = Gateway;
            SubmitPaymentForm("");
           
        }
        protected override void SubmitPaymentForm(string formContent)
        {

            string html = new WP_QuickPayUtils().createPayHtml(ValueVo,Plan);
            HttpContext.Current.Response.ContentType = "text/html;charset=" + WP_QucickPayConf.charset;
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(WP_QucickPayConf.charset);
            try
            {
                HttpContext.Current.Response.Write(html);
            }
            catch (Exception)
            {
            }
        }
        public override bool IsMedTrade
        {
            get { return false; }
        }

        public override void SendGoods(string tradeno, string logisticsName, string invoiceno, string transportType)
        {

        }

        public override string Description
        {
            get { return string.Empty; }
        }

        public override string Logo
        {
            get { return string.Empty; }
        }

        public override string ShortDescription
        {
            get { return string.Empty; }
        }
        
    }
}
